/**
 * LEKU APPLIANCE CHAINS.
 * Copyright (c) 2016-2016 All Rights Reserved.
 */
package com.leku.mse.service.impl.diary;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.leku.commons.datasource.DataSourceContextHolder;
import com.leku.commons.util.CollectionUtil;
import com.leku.commons.util.StringUtil;
import com.leku.mse.constants.DataSourceConstant;
import com.leku.mse.dao.diary.DiaryDao;
import com.leku.mse.entity.diary.AlbumBaseEntity;
import com.leku.mse.entity.diary.DiaryBaseEntity;
import com.leku.mse.entity.diary.DiaryElemEntity;
import com.leku.mse.entity.diary.DiaryOptEntity;
import com.leku.mse.mq.util.DiaryUtil;
import com.leku.mse.service.cache.DiaryCache;
import com.leku.mse.service.cache.SenseWordCache;
import com.leku.mse.service.diary.DiaryService;

/**
 * 功能描述: 手帐服务
 *
 * @author suiying
 * @created 2018年1月25日
 * @version 1.0.0
 */
@Service
public class DiaryServiceImpl implements DiaryService {

    @Autowired
    private DiaryCache diaryCache;

    @Autowired
    private SenseWordCache senseWordCache;

    @Autowired
    private DiaryDao diaryDao;

    /*
     * (non-Javadoc)
     * @see com.leku.mse.service.diary.DiaryService#dealEditDiary(com.leku.mse.entity.diary.DiaryOptEntity)
     */
    @Override
    public void dealEditDiary(DiaryOptEntity diaryOpt) {
        // 设置为手账数据库
        DataSourceContextHolder.setDataSourceType(DataSourceConstant.diaryDataSource());

        // 获取手账基本信息
        DiaryBaseEntity diaryBase = diaryDao.getDiaryBase(diaryOpt.getDiaryId());

        if (diaryBase == null) {
            return;
        }

        // 获取手账内容
        String data = diaryDao.getDiaryData(diaryOpt.getDiaryId());
        if (StringUtil.isEmpty(data)) {
            return;
        }

        DiaryElemEntity diaryElem = JSON.parseObject(data, DiaryElemEntity.class);

        // 推荐分数
        int recomScore = diaryCache.getRecomScore();
        boolean recomFlag = false;

        // 总的手账得分
        int totalScore = DiaryUtil.calDiaryScore(diaryElem.getData(), senseWordCache.getDiarySenseWord());

        if (totalScore >= recomScore) {
            recomFlag = true;
        }

        if (recomFlag && diaryBase.getIsPublic() == 1) {
            // 如果是推荐手账
            diaryBase.setScore(totalScore);
            diaryBase.setOrderNum(600);
        } else {
            // 如果是非推荐手账
            diaryBase.setScore(totalScore);
            diaryBase.setOrderNum(1000);
        }
        // 更新手账数据
        try {
            diaryDao.updateDiaryScore(diaryBase);
        } catch (Exception e) {
        }

        // 公开手帐
        if (diaryBase.getIsPublic() == 1) {
            // 新增手帐相关数据
            AddRelaDiary(diaryBase, recomFlag);
        } else {
            // 删除手帐相关数据
            removeRelaDiary(diaryOpt.getDiaryId());
        }

        // 更新
        try {
            diaryDao.updateDiaryEditStatus(diaryBase.getDiaryId());
        } catch (Exception e) {
        }

    }

    /*
     * (non-Javadoc)
     * @see com.leku.mse.service.diary.DiaryService#dealRemoveDiary(com.leku.mse.entity.diary.DiaryOptEntity)
     */
    @Override
    public void dealRemoveDiary(DiaryOptEntity diaryOpt) {
        // 设置为手账数据库
        DataSourceContextHolder.setDataSourceType(DataSourceConstant.diaryDataSource());

        // 删除手帐相关数据
        removeRelaDiary(diaryOpt.getDiaryId());

        // 更新手账删除处理状态
        try {
            diaryDao.updateDiaryDelStatus(diaryOpt.getDiaryId());
        } catch (Exception e) {
        }
    }

    /*
     * (non-Javadoc)
     * @see com.leku.mse.service.diary.DiaryService#dealMoveDiary(com.leku.mse.entity.diary.DiaryOptEntity)
     */
    @Override
    public void dealMoveDiary(DiaryOptEntity diaryOpt) {
        // 设置为手账数据库
        DataSourceContextHolder.setDataSourceType(DataSourceConstant.diaryDataSource());

        if (StringUtil.isNotEmpty(diaryOpt.getAlbumId())) {
            // 获取手账本基本信息
            AlbumBaseEntity albumBase = diaryDao.getAlbumBase(diaryOpt.getAlbumId());

            if (albumBase == null) {
                return;
            }

            if (albumBase.getIsPublic() == 0) {
                List<String> ids = diaryDao.queryDairyIds(diaryOpt.getAlbumId());

                if (CollectionUtil.isNotEmpty(ids)) {
                    for (String id : ids) {
                        // 删除手帐相关数据
                        removeRelaDiary(id);

                        // 更新手账转移处理状态
                        try {
                            diaryDao.updateDiaryMoveStatus(id);
                        } catch (Exception e) {
                        }
                    }
                }
            }
        } else if (StringUtil.isNotEmpty(diaryOpt.getDiaryId())) {
            // 获取手账基本信息
            DiaryBaseEntity diaryBase = diaryDao.getDiaryBase(diaryOpt.getDiaryId());

            if (diaryBase == null) {
                return;
            }

            if (diaryBase.getIsPublic() == 0) {
                // 删除手帐相关数据
                removeRelaDiary(diaryOpt.getDiaryId());
            }

            // 更新手账转移处理状态
            try {
                diaryDao.updateDiaryMoveStatus(diaryOpt.getDiaryId());
            } catch (Exception e) {
            }
        }
    }

    /**
     * 
     * 功能描述: 删除所有与手帐有关的表的数据
     *
     * @param diaryId
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private void removeRelaDiary(String diaryId) {
        // 设置为手账数据库
        DataSourceContextHolder.setDataSourceType(DataSourceConstant.diaryDataSource());

        // 删除热门手账列表
        try {
            diaryDao.deleteDiaryHot(diaryId);
        } catch (Exception e) {
        }

        // 删除推荐手账列表
        try {
            diaryDao.deleteDiarySysRecom(diaryId);
        } catch (Exception e) {
        }

        // 删除手账参与的兴趣组
        try {
            diaryDao.deleteDiaryTaste(diaryId);
        } catch (Exception e) {
        }

        // 删除手账搜索
        try {
            diaryDao.deleteDiarySearch(diaryId);
        } catch (Exception e) {
        }

        // 设置为手账数据库
        DataSourceContextHolder.setDataSourceType(DataSourceConstant.memDataSource());

        // 删除会员中心我的手帐
        try {
            diaryDao.deleteMemDiary(diaryId);
        } catch (Exception e) {
        }
    }

    /**
     * 
     * 功能描述: 新增所有与手帐有关的表的数据
     *
     * @param diaryBase
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private void AddRelaDiary(DiaryBaseEntity diaryBase, boolean recomFlag) {

        // 设置为手账数据库
        DataSourceContextHolder.setDataSourceType(DataSourceConstant.diaryDataSource());

        if (recomFlag) {
            // 更新热门手账列表
            try {
                diaryDao.insertDiaryHot(diaryBase);
            } catch (Exception e) {
            }

            // 更新推荐手账列表
            try {
                diaryDao.insertDiarySysRecom(diaryBase);
            } catch (Exception e) {
            }

            // 手账搜索
            try {
                int updNum = diaryDao.updateDiarySearch(diaryBase);
                if (updNum == 0) {
                    diaryDao.insertDiarySearch(diaryBase);
                }
            } catch (Exception e) {
            }

            // 更新手帐兴趣组
            try {
                diaryDao.updateDiaryTasteStatus(diaryBase.getDiaryId(), 1);
            } catch (Exception e) {
            }
        } else {
            // 更新手帐兴趣组
            try {
                diaryDao.updateDiaryTasteStatus(diaryBase.getDiaryId(), 2);
            } catch (Exception e) {
            }

            // 删除手账搜索
            try {
                diaryDao.deleteDiarySearch(diaryBase.getDiaryId());
            } catch (Exception e) {
            }
        }

        // 设置为手账数据库
        DataSourceContextHolder.setDataSourceType(DataSourceConstant.memDataSource());

        // 会员中心我的手帐
        try {
            int updNum = diaryDao.updateMemDiary(diaryBase);
            if (updNum == 0) {
                diaryDao.insertMemDiary(diaryBase);
            }
        } catch (Exception e) {
        }
    }
}
